package cn.sc.allinpay.allfavour.business.main;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.pai.common.adapter.BaseRecyAdapter;
import cn.pai.common.listener.OnPromptListener;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.base.activity.BaseActivity;
import cn.sc.allinpay.allfavour.business.cashiertrans.CashierTransActivity;
import cn.sc.allinpay.allfavour.business.cashiertransonline.CashierTransOnlineActivity;
import cn.sc.allinpay.allfavour.business.check.CheckActivity;
import cn.sc.allinpay.allfavour.business.common.adapter.MainMenuAdapter;
import cn.sc.allinpay.allfavour.business.common.data.MainMenuData;
import cn.sc.allinpay.allfavour.business.common.dialog.CashierVoidInputDialog;
import cn.sc.allinpay.allfavour.business.common.widget.InputAmtPopWindow;
import cn.sc.allinpay.allfavour.business.dist.DistActivity;
import cn.sc.allinpay.allfavour.business.set.SetActivity;

/**
 * activity 主界面
 * author：pany
 * on 2017/12/1 11:14
 */
public class MainActivity extends BaseActivity<MainContract.View, MainContract.Presenter>
        implements MainContract.View, InputAmtPopWindow.OnInputAmtListener, BaseRecyAdapter.OnAdapterListener<MainMenuData> {

    @BindView(R.id.tvStore)
    AppCompatTextView tvStore;
    @BindView(R.id.tvOperatorNo)
    AppCompatTextView tvOperatorNo;
    @BindView(R.id.tvOperator)
    AppCompatTextView tvOperator;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private InputAmtPopWindow amtPopWindow;

    @Override
    protected int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void layoutTitle(View top, AppCompatTextView left, AppCompatTextView title, AppCompatTextView right) {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exitApp();
            }
        });
        title.setText("万商惠");
    }

    @Override
    protected MainContract.Presenter bindPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void layoutContent(Bundle savedInstanceState) {
        recycler.setLayoutManager(new GridLayoutManager(this, 3));// 列表布局
        final List<MainMenuData> datas = new ArrayList<>();
        datas.add(new MainMenuData(R.mipmap.ic_flow, "交易流水"));
        datas.add(new MainMenuData(R.mipmap.ic_flow, "快捷加油流水"));
        datas.add(new MainMenuData(R.mipmap.ic_dist, "发券"));
//        datas.add(new MainMenuData(R.mipmap.ic_send, "售券"));
        datas.add(new MainMenuData(R.mipmap.ic_verify, "验券"));
        datas.add(new MainMenuData(R.mipmap.ic_void, "撤销"));
        datas.add(new MainMenuData(R.mipmap.ic_manage, "管理"));
        MainMenuAdapter menuAdapter = new MainMenuAdapter(datas);
        menuAdapter.setOnAdapterListener(this);
        recycler.setAdapter(menuAdapter);
        amtPopWindow = new InputAmtPopWindow(this);
        amtPopWindow.setOnInputAmtListener(this);
    }

    @OnClick({R.id.btCashier})
    void btnClick(View view) {
        switch (view.getId()) {
            case R.id.btCashier:
                if (!presenter.isPosInfoExist()) {
                    prompt("未开通此功能");
                    return;
                }
                presenter.checkOrder(view);
                break;
        }
    }

    @Override
    public void showStore(String data) {
        tvStore.setText("  " + data);
    }

    @Override
    public void showOperatorNo(String data) {
        tvOperatorNo.setText("  " + data);
    }

    @Override
    public void showOperator(String data) {
        tvOperator.setText(data);
    }

    @Override
    public void showPay(View view) {
        amtPopWindow.show(view, "收银金额");
    }

    @Override
    public void onInputAmt(String inputAmt) {
        presenter.startCashierActivity(inputAmt);
    }

    @Override
    public void onItemClick(View view, int position, MainMenuData data) {
        switch (position) {
            case 0://交易流水
                if (!presenter.isPosInfoExist()) {
                    prompt("未开通此功能");
                    return;
                }
                toActivity(CashierTransActivity.class, null);
                break;
            case 1://快捷加油流水
                if (!presenter.isPosInfoExist()) {
                    prompt("未开通此功能");
                    return;
                }
                toActivity(CashierTransOnlineActivity.class, null);
                break;
            case 2://发券
                toActivity(DistActivity.class, null);
                break;
//            case 3://售券
//                if (!presenter.isPosInfoExist()) {
//                    prompt("未开通此功能");
//                    return;
//                }
//                toActivity(SaleActivity.class, null);
//                break;
            case 3://验券
                toActivity(CheckActivity.class, null);
                break;
            case 4://撤销
                CashierVoidInputDialog dialog = new CashierVoidInputDialog(this);
                dialog.setOnDialogListener(new CashierVoidInputDialog.OnDialogListener() {
                    @Override
                    protected void onConfirm(String input) {
                        Bundle bundle = new Bundle();
                        bundle.putString(Constant.BundleKey.INPUTORDERID, input);
                        toActivity(CashierTransActivity.class, bundle);
                    }
                });
                dialog.show();
                break;
            case 5:
                toActivity(SetActivity.class, null);
                break;
        }
    }

    private void exitApp() {
        prompt(true, "您是否退出万商惠？", new OnPromptListener("确认", "取消") {
            @Override
            public void onNegative(DialogInterface dialog, int which) {
                dialog.dismiss();
            }

            @Override
            public void onPositive(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
                System.exit(0);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
